﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections;

namespace JsonTest1.Data
{
    public enum PlaylistPlaybackMode
    {
        NORMAL, SHUFFLE, REPEAT, SHUFFLE_AND_REPEAT
    }

    public class Playlist
    {


        /**
	 * Keep order in which tracks will be play
	 */
        private List<int> mPlayOrder = new List<int>();
        //private ArrayList<Integer> mPlayOrder = new ArrayList<Integer>();


        	/* Keeps playlist's entries
	 */
        protected List<PlaylistEntry> playlist = null;

        protected int selected = -1;

        /**
         * Keep playlist playback mode
         */
        private PlaylistPlaybackMode mPlaylistPlaybackMode = PlaylistPlaybackMode.NORMAL;


        public Playlist()
        {
            playlist = new List<PlaylistEntry>();
            calculateOrder(true);
        }

        public int size()
        {
            return playlist == null ? 0 : playlist.Count;
        }

        private void calculateOrder(bool force)
        {
            if (mPlayOrder.Count==0 || force)
            {
                int oldSelected = 0;

                if (!(mPlayOrder.Count==0))
                {
                    oldSelected = mPlayOrder[selected];
                    mPlayOrder.Clear();
                }

                for (int i = 0; i < size(); i++)
                {
                    mPlayOrder.Insert(i,i);
                }

                if (mPlaylistPlaybackMode == null)
                {
                    mPlaylistPlaybackMode = PlaylistPlaybackMode.NORMAL;
                }



                switch (mPlaylistPlaybackMode)
                {
                    case PlaylistPlaybackMode.NORMAL:
                    case PlaylistPlaybackMode.REPEAT:
                        selected = oldSelected;
                        break;
                    case PlaylistPlaybackMode.SHUFFLE:
                    case PlaylistPlaybackMode.SHUFFLE_AND_REPEAT:
                        /*System.Collections.shuffle(mPlayOrder);
                        selected = mPlayOrder.indexOf(selected);*/
                        break;
                }
            }
        }


        /**
	 * Adds PlaylistEntry object to the playlist
	 * 
	 * @param playlistEntry
	 */
        public void addPlaylistEntry(PlaylistEntry playlistEntry)
        {
            if (playlistEntry != null)
            {
                playlist.Add(playlistEntry);
                mPlayOrder.Add(size() - 1);
            }
        }

    }
}
